
cd $firmpath 

use $outputpath/table_OA11, clear 
drop if type=="sd"
drop if variable=="eta" 
g one=1
reshape wide g_all entri exitc intens, i(one) j(variable)s
save tmp, replace 


use $correspath/corres_nc819962010.dta, clear // 19952010.dta, clear 
cap g nc8 = cn8
egen _=count(nc8), by(id_conc year) 
sort id_conc year
bys id_conc: egen nbm=max(_)
keep if year==2002 
tostring nc8, replace 
replace nc8="0"+nc8 if length(nc8)==7
g hs6_2002 =substr(nc8,1,6) if length(nc8)==8  

keep hs6 id_conc 
destring hs6, replace
merge m:1 hs6_2002 using $rspath/rs_hs02_19962006_controls 
collapse (mean) rs_hs6_b frac_lib_not_homog lup lsigma pci (max) dif_rauch , by(id_conc)
save tmp2, replace 

use decompo_margin_$firstyear$lastyear, clear
duplicates drop id_conc, force 
merge 1:1 id_conc using tmp2  
keep if _m==3 
xtile decile_=rs_hs6_b , n(10)
collapse (mean) rs, by(decile_)
g one=1 
merge m:1 one using tmp


g all=g_alluncertainty + rs*g_alleta_uncert
g entry=entriuncertainty + rs*entrieta_uncert
g exit=exitcuncertainty + rs*exitceta_uncert
g intens=intensuncertainty + rs*intenseta_uncert

g allc=g_allcrisis + rs*g_alleta_crisis
g entryc=entricrisis + rs*entrieta_crisis
g exitc=exitccrisis + rs*exitceta_crisis
g intensc=intenscrisis + rs*intenseta_crisis


gr twoway (scatter  allc exitc entryc intensc decile_, m(S O O Oh) msize(large) mc(gs8 green%30 red%30 blue%70) msize(medium medium vlarge vlarge) legend(on order(1 "Total" 2 "Separation" 3 "New" 4 "Intensive" ) lstyle(none) lc(gs16) c(4)))  (spike allc decile_, lw(large) lp(dot) sort), ytitle("") xtitle("") xlabel(1(1)10) ylabel(,angle(0))
graph export $outputpath/figure3_growth.pdf, replace

gr twoway (scatter  all exit entry intens decile_, m(S O O Oh) msize(large) mc(gs8 green%30 red%30 blue%70) msize(medium medium vlarge vlarge) legend(on order(1 "Total" 2 "Separation" 3 "New" 4 "Intensive" ) lstyle(none) lc(gs16) c(4)))  (spike all decile_, lw(large) lp(dot) sort), ytitle("") xtitle("") xlabel(1(1)10) ylabel(, angle(0) )
graph export $outputpath/figure3_uncertainty.pdf, replace

erase tmp.dta 
erase tmp2.dta 
